#ifndef MODEL_H
#define MODEL_H
#include "global_object.h"
#include "Geometry.cu"
#include "Texture.cu"
#include "Drawable.cu"
#include "Material.cu"
#include "SampleRequest.cu"
#include "SampleResult.cu"
class Model : public Drawable
{
public:
	inline __host__ __device__  Model(void){
		size = sizeof(Model);
	}
	inline __host__ __device__ ~Model(void);

	Texture* texture;
	Material* material;
	Geometry* geometry;

	inline __device__  SampleResult sample(SampleRequest& req){
		IntersectRequest ireq;
		ireq.tMin = req.tMin;
		ireq.tMax = req.tMax;
		ireq.ray = req.ray;
		IntersectResult ires = geometry->intersect(ireq);
		SampleResult sres;
		sres.ir = ires;
		if(ires.hit){
			sres.color = material->diffuse;
			sres.weightRemaining = 0.0f;
		}
		else {
			sres.color = Vector3(0.1f, 0.1f, 0.1f); //background
			sres.weightRemaining = 0.0f;
		}
		return sres;
	}

	inline __host__ void copyToDevice() {
		if(d_address == NULL){
			Model d_mdl = Model(*this);
			if(texture->d_address == NULL){
				texture->copyToDevice();
				d_mdl.texture = (Texture*)texture->d_address;
			}
			if(material->d_address == NULL){
				material->copyToDevice();
				d_mdl.material = (Material*)material->d_address;
			}
			if(geometry->d_address == NULL){
				geometry->copyToDevice();
				d_mdl.geometry = (Geometry*)geometry->d_address;
			}
			cudaMemcpy(d_address, &d_mdl, sizeof(Model), cudaMemcpyHostToDevice);
		}
	}


};

#endif